package com.example.memory.util;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.LinkedList;
import java.util.List;

public class FileUtils {

    public static final String DOC = "doc";
    public static final String DOCX = "docx";
    public static final String ZIP = "zip";
    public static final String PDF = "pdf";
    public static final String XLS = "xls";
    public static final String XLSX = "xlsx";
    public static final String POINT = ".";
    public static String HOME = System.getProperty("user.dir");
    public static String NEW_LINE = System.getProperty("line.separator");
    public static String[] imgs = {"webp", "bmp", "pcx", "tif", "gif", "jpg", "jpeg", "tga", "exif", "fpx", "svg", "psd",
            "cdr", "pcd", "dxf", "ufo", "eps", "ai", "png", "hdri", "raw", "wmf", "flic", "emf",
            "ico", "avif", "apng"};
    public static String[] videos = {"wmv", "asf", "asx", "rm", " rmvb", "mp4", "3gp", "mov", "m4v", "avi",
            "dat", "mkv", "flv", "vob"};

    public static String img_video(String name) {
        String lower = name.toLowerCase();
        for (String img : imgs) {
            if (lower.endsWith(img)) {
                return "img";
            }
        }
        for (String video : videos) {
            if (lower.endsWith(video)) {
                return "video";
            }
        }
        return null;
    }


    public static String getPath(String... paths) {
        return String.join(File.separator, paths) + File.separator;
    }

    public static String getSuffix(String name) {
        return name.substring(name.lastIndexOf(".") + 1);
    }

    /**
     * 将inputStream流缓存到本地path
     *
     * @param inputStream
     * @param path
     */
    public static File writeFile(InputStream inputStream, String path, String name) {
        File file;
        try {
            file = createFile(path, name);
            Files.copy(inputStream, Paths.get(path, name), StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            return null;
        }
        return file;
    }

    public static File createFile(String path, String name) throws IOException {
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(path + File.separator + name);
        if (!file.exists()) {
            file.createNewFile();
        }
        return file;
    }

    public static File createFile(File file) throws IOException {
        if (!file.exists()) {
            return createFile(file.getParent(), file.getName());
        }
        return file;
    }

    public static File writeFile(byte[] data, String path, String name) {
        InputStream inputStream = new ByteArrayInputStream(data, 0, data.length - 1);
        return writeFile(inputStream, path, name);
    }

    public static <T> File writeObject(List<T> data, String path, String name) {
        File file = new File(path + File.separator + name);
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
            for (T t : data) {
                writer.write(t.toString());
                writer.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    public static List<String> readInputStream(InputStream inputStream) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        List<String> res = new LinkedList<>();
        String temp;
        try {
            while ((temp = reader.readLine()) != null) {
                res.add(temp);
            }
            FunctionUtils.closeQuietly(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }

    public static List<String> readFile(String path) throws FileNotFoundException {
        return readInputStream(new FileInputStream(path));
    }

    public static List<String> readFile(File file) throws FileNotFoundException {
        return readInputStream(new FileInputStream(file));
    }


    public static InputStream getIns(String path, String filename) {
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(path + "\\" + filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return inputStream;
    }

    public static InputStream getIns(File file) {
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return inputStream;
    }

    public static String getFileSuffix(String filename) {
        int pos = filename.lastIndexOf(".");
        return filename.substring(pos + 1);
    }

    public static void createDir(String path) {
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }
    }

    public static void move(File file, String dist) {
        file.renameTo(new File(dist + file.getName()));
    }

    public static boolean exists(String dist, File file) {
        File[] files = new File(dist).listFiles();
        for (File temp : files) {
            if (temp.equals(file)) {
                return true;
            }
        }
        return false;
    }

    public static boolean exist(String dist, String name) {
        return Files.exists(Paths.get(dist, name), LinkOption.NOFOLLOW_LINKS);
    }
}
